package com.trimps.wifi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trimps.wifi.common.Constants;
import com.trimps.wifi.common.Paginator;
import com.trimps.wifi.pojo.Account;
import com.trimps.wifi.pojo.FlowData;
import com.trimps.wifi.service.IDataService;
import com.trimps.wifi.service.SnifferDataService;

@Controller
@RequestMapping(value = "data/")
public class SnifferDataController extends BaseController {
    private static final Logger logger = Logger.getLogger(SnifferDataController.class);

    @Autowired
    private IDataService dataService;
    @Autowired
    private SnifferDataService snifferDataService;

    @RequestMapping(value = "account")
    public String account(String keyWord, Paginator page,ModelMap map) {
    	if(page == null || page.getTotalItemCount() == -1){
    		page = new Paginator();
    		page.setShowItemCount(8);
    		int userCount = dataService.getAccountCount(keyWord);
    		page.setTotalItemCount(userCount);
    	}
    	Map<String, Object> accounts = dataService.getAllAccount(keyWord, page.getStart(), page.getOffset());
    	map.put("accounts", accounts);
    	map.put("page", page);
    	map.put("keyWord", keyWord);
    	return "data/account";
    }
    
    
    @RequestMapping(value = "detail")
    public String detail(@RequestParam("terMac") String terMac, ModelMap map) {
    	return "data/detail";
    }
    
    /**
     * 获取列表
     * @param page  这里必须不能写@RequstParam注解，否则不能正常映射
     * @param map
     * @return
     */
    @RequestMapping(value = "flow")
    public String userlist() {
        return "data/flow";
    }
    
    /**
     * 获取列表信息
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "flowList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getFlowListByKeyword(@RequestParam("curPage") String cp, @RequestParam("totalPage") String tp,
                    @RequestParam(value="deviceMac", required = false) String deviceMac,
                    @RequestParam(value="deviceNo", required = false) String deviceNo,
                    @RequestParam(value="startTime", required = false) String startTime,
                    @RequestParam(value="endTime", required = false) String endTime)  {
     // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        int curPage = Constants.DEFAULT_FIRST_PAGE, totalCount, totalPage;
        List<FlowData> lst = new ArrayList<FlowData>();
        // if ("".equals(tp)) {
        totalCount = snifferDataService.getFlowListCountByKeyword(deviceMac, deviceNo, startTime, endTime);
        if (totalCount == 0) {
            totalPage = 0;
        } else {
            // 计算totalpage
            totalPage = (int) Math.ceil((double) totalCount / (double) Constants.RECORD_PER_PAGE);
        }

        if (totalPage != 0) {
            if (!("".equals(cp))) {
                curPage = Integer.parseInt(cp);
            }
            lst = snifferDataService.getFlowListByKeyword(deviceMac, deviceNo, startTime, endTime, curPage, Constants.RECORD_PER_PAGE);
        }

        map.put("totalPage", totalPage);
        map.put("currentPage", curPage);
        map.put("listInfo", lst);
        map.put("totalCount", totalCount);
        return map;
    }
    
    
}
